/**
 * 				Sound.java
 *				------------
 *		
 *   Author:	bin04017@gmail.com
 *	 Created:	13.12.2006
 *	 Project:	MP3-Sync (Mochito)
 *	 Package:	mochito.sound
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.		
 */
package mochito.sound;

import java.io.File;
import java.util.TimerTask;

import mochito.dataClasses.musicItem.MusicItem;

/**
 * @author bin04017
 * 
 */
public class SoundPlayer extends TimerTask {

	private File file = null;

	private Thread soundThread = null;

	private boolean statusPlay = false;

	private MusicItem item = null;

	public SoundPlayer(MusicItem item) {
		this.file = item.getFile();
		this.item = item;
	}

	public SoundPlayer(File file) {
		this.file = file;
	}

	public SoundPlayer(String fileName) {
		this.file = new File(fileName);
	}

	private void makeSoundThread() {
		if (this.soundThread == null) {
			this.soundThread = new Thread(new SoundItem(file));
		}
	}

	public void play() {
		if (!statusPlay) {
			this.makeSoundThread();
			this.soundThread.start();
			statusPlay = true;
		}
	}

	@SuppressWarnings("deprecation")
	public void stop() {
		if (statusPlay) {
			this.soundThread.stop();
			this.soundThread = null;
			statusPlay = false;
		}
	}

	/**
	 * @return the file
	 */
	public File getFile() {
		return file;
	}

	/**
	 * @return the statusPlay
	 */
	public boolean isStatusPlay() {
		return statusPlay;
	}

	/**
	 * 
	 * @return hasMusicItem
	 */
	public boolean hasMusicItem() {
		return (item != null);
	}

	/**
	 * @return the item
	 */
	public MusicItem getItem() {
		return item;
	}

	public void run() {
		this.play();
	}
}
